package common.code;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.velocity.tools.config.Data;

public class DateUtils
{
  private static String[] dateFormat = { "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy年MM月dd日HH时mm分ss秒", "yyyy-MM-dd", "yyyy/MM/dd", "yy-MM-dd", "yy/MM/dd", "yyyy年MM月dd日", "HH:mm:ss", "yyyyMMddHHmmss", "yyyyMMdd", "yyyy.MM.dd", "yy.MM.dd", "MM月dd日HH时mm分","mm:ss" };

  public static Timestamp convUtilCalendarToSqlTimestamp(Calendar date)
  {
    if (date == null)
      return null;

    return new Timestamp(date.getTimeInMillis());
  }

  public static Calendar convSqlTimestampToUtilCalendar(Timestamp date)
  {
    if (date == null)
      return null;

    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(date.getTime());
    return gc;
  }

  public static Calendar parseDate(String dateStr)
  {
    if ((dateStr == null) || (dateStr.trim().length() == 0))
      return null;

    Date result = parseDate(dateStr, 0);
    Calendar cal = Calendar.getInstance();
    cal.setTime(result);

    return cal;
  }

  public static String toDateTimeStr(Calendar date)
  {
    if (date == null)
      return null;
    return new SimpleDateFormat(dateFormat[0]).format(date.getTime());
  }

  public static String toDateTimeStr(int format, Calendar date)
  {
    if (date == null)
      return null;

    return new SimpleDateFormat(dateFormat[format]).format(date.getTime());
  }

  public static String toDateStr(Calendar date)
  {
    if (date == null)
      return null;
    return new SimpleDateFormat(dateFormat[3]).format(date.getTime());
  }

  public static String toDateStrByFormatIndex(Calendar date, int formatIndex) {
    if (date == null)
      return null;
    return new SimpleDateFormat(dateFormat[formatIndex]).format(date.getTime());
  }
  
  public static String toStrByIndex(Date date, int formatIndex) {
    if (date == null)
      return null;
    return new SimpleDateFormat(dateFormat[formatIndex]).format(date.getTime());
  }

  public static int calendarMinus(Calendar d1, Calendar d2) {
    if ((d1 == null) || (d2 == null)) {
      return 0;
    }

    d1.set(11, 0);
    d1.set(12, 0);
    d1.set(13, 0);

    d2.set(11, 0);
    d2.set(12, 0);
    d2.set(13, 0);

    long t1 = d1.getTimeInMillis();
    long t2 = d2.getTimeInMillis();

    long daylong = 86400000L;
    t1 -= t1 % daylong;
    t2 -= t2 % daylong;

    long t = t1 - t2;
    int value = (int)(t / daylong);


    return value;
  }

  public static long calendarminus(Calendar d1, Calendar d2)
  {
    if ((d1 == null) || (d2 == null))
      return 0L;

    return ((d1.getTimeInMillis() - d2.getTimeInMillis()) / 86400000L);
  }

  public static Date parseDate(String dateStr, int index)
  {
    DateFormat df = null;
    try {
      df = new SimpleDateFormat(dateFormat[index]);

      return df.parse(dateStr);
    } catch (ParseException pe) {
      return parseDate(dateStr, index + 1);
    } catch (ArrayIndexOutOfBoundsException aioe) {
      return null;
    }
  }

  public static Date StringToDate(String dateStr)
  {
    if ((dateStr == null) || (dateStr.trim().length() == 0))
      return null;

    return parseDate(dateStr, 3);
  }

  public static String dateToString(Date date, int index)
  {
    if (date == null)
      return null;

    return new SimpleDateFormat(dateFormat[index]).format(date);
  }

  public static String dateToString(Date date)
  {
    if (date == null)
      return null;

    return new SimpleDateFormat(dateFormat[3]).format(date);
  }

  public static Timestamp convUtilDateToSqlTimestamp(Date date)
  {
    if (date == null)
      return null;

    return new Timestamp(date.getTime()); }

  public static Calendar convUtilDateToUtilCalendar(Date date) {
    if (date == null)
      return null;

    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(date.getTime());
    return gc;
  }

  public static Timestamp parseTimestamp(String dateStr, int index)
  {
    DateFormat df = null;
    try {
      df = new SimpleDateFormat(dateFormat[index]);

      return new Timestamp(df.parse(dateStr).getTime());
    } catch (ParseException pe) {
      return new Timestamp(parseDate(dateStr, index + 1).getTime());
    } catch (ArrayIndexOutOfBoundsException aioe) {
      return null;
    }
  }

  public static Timestamp parseTimestamp(String dateStr)
  {
    DateFormat df = null;
    try {
      df = new SimpleDateFormat(dateFormat[3]);
      return new Timestamp(df.parse(dateStr).getTime());
    } catch (ParseException pe) {
      return null;
    } catch (ArrayIndexOutOfBoundsException aioe) {
      return null;
    }
  }
  
  /**
   * 获得指定日期的前一天
   */
  public static String getSpecifiedDayBefore(String specifiedDay) {
      Calendar c = Calendar.getInstance();
      Date date = null;
      try {
          date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
      } catch (ParseException e) {
          e.printStackTrace();
      }
      c.setTime(date);
      int day = c.get(Calendar.DATE);
      c.set(Calendar.DATE, day - 1);
      String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
      return dayBefore;
  }
  
  /**
   * 获得指定日期的后一天
   * 
   * @param specifiedDay
   * @return
   */
  public static String getSpecifiedDayAfter(String specifiedDay) {
      Calendar c = Calendar.getInstance();
      Date date = null;
      try {
          date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
      } catch (ParseException e) {
          e.printStackTrace();
      }
      c.setTime(date);
      int day = c.get(Calendar.DATE);
      c.set(Calendar.DATE, day + 1);
      String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
      return dayAfter;
  }
  
  /**
   * 指定日期的下几天或者上几天是多少号
   *
   * @param inputTime
   * @param nextCount 可以为负数
   * @return
   */
  public static String getNextDay(Date inputTime, int nextCount) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      // Date now = new Date();
      String[] time = format.format(inputTime).split("-");
      int year = Integer.parseInt(time[0]);
      int month = Integer.parseInt(time[1]);
      int day = Integer.parseInt(time[2]);
      Calendar can = Calendar.getInstance();
      can.set(year, month - 1, day);
      can.add(Calendar.DAY_OF_MONTH, nextCount);
      return format.format(can.getTime());
  }
  
  public static void main(String[] args) {
//	  getSpecifiedDayAfter("2013-11-30");
//	  System.out.println(getSpecifiedDayAfter("2013-11-30"));
	  String time = getNextDay(new Date(), -20);
//	  System.out.println("time----"+time);
	  
	  String DateStr1 = "2011-10-07 15:50:35";
	  String DateStr2 = "2011-10-07 15:50:35";
	  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	try {
		  Date dateTime1 = dateFormat.parse(DateStr1);
		  Date dateTime2 = dateFormat.parse(DateStr2);
		  Date nowDate1 = new Date();
		  Date nowDate = new Date();
		  System.out.println("nowDate----"+nowDate);
		  int i = nowDate1.compareTo(nowDate); 
		  String nowString = DateUtils.dateToString(nowDate);
		  System.out.println("nowDate----"+nowString);
		  nowDate = DateUtils.StringToDate(nowString);
		  System.out.println("nowDate----"+nowDate);
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

  }
}